package cn.bulgat.spring.web.exception;

import cn.bulgat.common.exception.CustomException;
import cn.bulgat.common.model.BaseResp;
import cn.bulgat.common.model.RespCode;
import cn.bulgat.common.utils.RespUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public BaseResp<?> customExceptionHandler(CustomException e) {
        log.error("CustomException", e);
        return RespUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResp<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return RespUtils.error(RespCode.SYSTEM_ERROR, "系统错误");
    }
}
